home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d939.lha / ExtraCmds / source_etc.lha / src / Head.c < prev    next >
C/C++ Source or Header  |  1993-10-22  |  5KB  |  210 lines

  1. /*   ---------------------------------      -------     
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  Head - Display first few lines of specified files.
  7.  *  Copyright (C) 1992, 1993 Torsten Poulin
  8.  *
  9.  *  This program is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program; if not, write to the Free Software
  21.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *  The author can be contacted by s-mail at
  24.  *    Torsten Poulin
  25.  *    Banebrinken 99, 2, 77
  26.  *    DK-2400 Copenhagen NV
  27.  *    DENMARK
  28.  *
  29.  * Created 11-Feb-92 Version 37.1 =TP=
  30.  * $Id: Head.c,v 37.7 93/03/01 12:36:57 Torsten Rel $
  31.  * $Log:    Head.c,v $
  32.  * Revision 37.7  93/03/01  12:36:57  Torsten
  33.  * Changed all occurrences of "struct DosBase *" to "struct DosLibrary *"
  34.  * 
  35.  * Revision 37.6  93/02/24  00:13:13  Torsten
  36.  * Bug fix: called FreeArgs() too soon.
  37.  * The TO argument is now functional.
  38.  * Eliminated goto statement in entrypoint().
  39.  * Removed unnecessary auto variable err in head().
  40.  * 
  41.  * Revision 37.5  93/02/22  17:37:22  Torsten
  42.  * Moved some functions to the support library.
  43.  * 
  44.  * Revision 37.4  93/02/20  11:51:12  Torsten
  45.  * head() now returns the correct value, if a Ctrl-C is recieved. 
  46.  * 
  47.  * Revision 37.3  93/02/20  11:45:08  Torsten
  48.  * Rewritten almost from scratch.
  49.  * 
  50.  * Revision 37.2  93/02/13  15:56:39  Torsten
  51.  * Reformatted source.
  52.  * Replaced exec.library/SetSignal() with dos.library/CheckSignal().
  53.  * 
  54.  */
  55.  
  56. #include <exec/types.h>
  57. #include <exec/memory.h>
  58. #include <dos/dos.h>
  59. #include <dos/dosasl.h>
  60. #include <clib/dos_protos.h>
  61. #include <clib/exec_protos.h>
  62. #ifdef __SASC
  63. #include <pragmas/dos_pragmas.h>
  64. #include <pragmas/exec_pragmas.h>
  65. #endif
  66. #include "tastlib.h"
  67. #include "head_rev.h"
  68.  
  69. #define PROGNAME "Head"
  70. #define TEMPLATE "FROM/M,TO/K,NUM=NUMBER/N/K"
  71. #define OPT_FROM 0
  72. #define OPT_TO   1
  73. #define OPT_NUM  2
  74.  
  75. char const versionID[] = VERSTAG;
  76. char const copyright[] = "$COPYRIGHT:Copyright © 1992,1993 Torsten Poulin$";
  77.  
  78. typedef struct {
  79.   struct DosLibrary *DOSBase;
  80.   ULONG number;
  81.   BOOL  header;
  82.   BPTR  output;
  83. } Global;
  84.  
  85. LONG head(UBYTE *filename, Global *global);
  86.  
  87.  
  88. LONG entrypoint(VOID)
  89. {
  90.   struct DosLibrary *DOSBase;
  91.   struct RDArgs *args;
  92.   Global *global;
  93.   LONG arg[3];
  94.   LONG rc = RETURN_OK;
  95.  
  96.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  97.     return RETURN_FAIL;
  98.  
  99.   if (!(global = AllocVec(sizeof(Global), MEMF_PUBLIC | MEMF_CLEAR)))
  100.   {
  101.     PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
  102.     rc = RETURN_FAIL;
  103.   }
  104.   else
  105.   {
  106.     global->DOSBase = DOSBase;
  107.  
  108.     arg[OPT_FROM] = arg[OPT_TO] = arg[OPT_NUM] = 0L;
  109.     
  110.     if (!(args = ReadArgs(TEMPLATE, arg, NULL)))
  111.     {
  112.       printerror(PROGNAME, global);
  113.       rc = RETURN_ERROR;
  114.     }
  115.     else
  116.     {
  117.       if (!arg[OPT_NUM])
  118.     global->number = 10L;
  119.       else
  120.       {
  121.     global->number = *(ULONG *) arg[OPT_NUM];
  122.     if (global->number < 1L) 
  123.       global->number = 1L;
  124.       }
  125.  
  126.       if (!arg[OPT_TO])
  127.     global->output = Output();
  128.       else if (!(global->output = Open((UBYTE *)arg[OPT_TO], MODE_NEWFILE)))
  129.       {
  130.     PutStr("Cannot open ");
  131.     PutStr((UBYTE *) arg[OPT_TO]);
  132.     PutStr("\n");
  133.     rc = RETURN_ERROR;
  134.       }
  135.     
  136.       if (global->output)
  137.     if (!arg[OPT_FROM])
  138.       rc = head(NULL, global);
  139.     else if ((rc = severalnames((UBYTE **) arg[OPT_FROM], global))
  140.          != ERROR_NO_FREE_STORE)
  141.     {
  142.       global->header = (BOOL) rc;
  143.       rc = foreach((UBYTE **) arg[OPT_FROM], head, global);
  144.     }
  145.       if (arg[OPT_TO])
  146.     Close(global->output);
  147.  
  148.       FreeArgs(args);
  149.  
  150.       if (rc == ERROR_BREAK)
  151.       {
  152.     PrintFault(ERROR_BREAK, NULL);
  153.     rc = RETURN_WARN;
  154.       }
  155.       else if (rc == ERROR_NO_FREE_STORE)
  156.       {
  157.     PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
  158.     rc = RETURN_FAIL;
  159.       }
  160.       else if (rc != RETURN_OK)
  161.     printerror(PROGNAME, global);
  162.     }
  163.     FreeVec(global);
  164.   }
  165.   CloseLibrary((struct Library *) DOSBase);
  166.   return rc;
  167. }
  168.  
  169.  
  170. LONG head(UBYTE *filename, Global *global)
  171. {
  172.   struct DosLibrary *DOSBase = global->DOSBase;
  173.   register UBYTE breakcheck = 0;
  174.   LONG  c;
  175.   ULONG count = 0;
  176.   LONG  rc = RETURN_OK;
  177.   BPTR  input = Input();
  178.     
  179.   if (filename)
  180.   {
  181.     if (global->header)
  182.     {
  183.       FPuts(global->output, "==>");
  184.       FPuts(global->output, filename);
  185.       FPuts(global->output, "<==\n");
  186.     }
  187.     if (!(input = Open(filename, MODE_OLDFILE)))
  188.     {
  189.       PrintFault(IoErr(), filename);
  190.       return RETURN_ERROR;
  191.     }
  192.   }
  193.  
  194.   while ((c = FGetC(input)) != EOF && count < global->number)
  195.   {
  196.     if (c == '\n')
  197.       ++count;
  198.     FPutC(global->output, c);
  199.  
  200.     if (!(breakcheck -= 4) && CheckSignal(SIGBREAKF_CTRL_C))
  201.     {
  202.       rc = ERROR_BREAK;
  203.       break;
  204.     }
  205.   }
  206.   if (filename)
  207.     Close(input);
  208.   return rc;
  209. }
  210.